package com.tanhua.api.mongo;


import com.tanhua.mongo.RecommendUser;
import com.tanhua.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.net.Socket;
import java.util.List;

@Service
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        Query query = new Query(Criteria.where("toUserId").is(toUserId));
        query.with(new Sort(Sort.Direction.DESC,"score"));
        query.skip(0).limit(1);
        return mongoTemplate.findOne(query,RecommendUser.class);
    }

    @Override
    public PageBeanVo queryTodayBestList(Long userID, Integer page, Integer pageSize) {
        Query query = new Query(Criteria.where("toUserId").is(userID));
        query.with(new Sort(Sort.Direction.DESC,"score"));
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        long count = mongoTemplate.count(query, RecommendUser.class);
        PageBeanVo pageBeanVo = new PageBeanVo();
        pageBeanVo.setCounts(count);
        pageBeanVo.setItems(recommendUsers);
        pageBeanVo.setPage(page);
        pageBeanVo.setPagesize(pageSize);
        return pageBeanVo;



    }

    @Override
    public Long findScore(Long jRid, Long userId) {

        Query query = new Query(Criteria.where("userId").is(jRid).and("toUserId").is(userId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser==null){
            return 67L;
        }
        return recommendUser.getScore().longValue();
    }
}
